/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Text,TextArea, Input,CCell, Table, TableBody, TableRow, TableHead, Cell,
} from "customize-easy-ui-component";
import {InspectRecordLayout, InternalItemProps, SelectHookfork, useItemInputControl,} from "../../common/base";
import {useMeasureInpFilter} from "../../common/hooks";
import {calcAverageArrObj, floatInterception, tableSetInp} from "../../../common/tool";
import {RepLink} from "../../common/base";

//升降机， 特别点： 布局也新的做法！
export const config运行速度=[ ['笼升速表','吊笼提升速度', '额定速度'], ];
interface Props  extends InternalItemProps{
    config: any[];
    sseq: number;
}
export const itemA速度 = ['速度备注'];
export const MoveSpeedLift =
    React.forwardRef((
        {children, show, alone = true, refWidth,label,config,sseq}: Props, ref
    ) => {
        const config上 = config.slice(0, sseq);
        const config下 = config.slice(sseq);
        const itemA = React.useMemo(() => {
            let items: string[]=[];
            config.forEach(([name,title, ratednm], i:number)=>{
                items.push(name+'',  name+'r');
                items.push(ratednm);
            });
            return [...items, ...itemA速度];
        }, [config]);
        const [getInpFilter] = useMeasureInpFilter(null, itemA,);
        const {inp, setInp} = useItemInputControl({ ref });
        return (
            <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                                 alone={alone} label={label!}>
                <Text variant="h5">{label}</Text>
                <Text variant="h5">{config上.length} 个项目: 运行距离（m）{'>>'}</Text>
                <div css={{display: 'flex', margin: 'auto'}}>
                    <div css={{display: 'inline-block', margin: 'auto'}}>
                        {config上.map(([name,title, ratednm], t: number) => {
                            const avspeed=calcAverageArrObj(inp?.[name],(row)=>{return row?.d/row?.t},2);
                            let percnt=avspeed!==undefined? Math.abs((Number(avspeed) - inp?.[ratednm]))*100/inp?.[ratednm] : 0;
                            return <React.Fragment key={t}>
                                <Text variant="h5">{t+1},&nbsp; {title} :</Text>
                                {(new Array(3)).fill(null).map((_, i: number) => {
                                    let o = inp?.[name]?.[i];
                                    const itspd = o?.d / o?.t;
                                    return <div key={i}>
                                        <Text>&nbsp; {i + 1} 次: </Text>
                                        距离
                                        <Input value={o?.d || ''} size={4}
                                               style={{display: 'inline-flex', width: 'unset'}}
                                               onChange={e => tableSetInp(name, i, inp, setInp, 'd', e.currentTarget.value || undefined)}/>
                                        (m)，时间
                                        <Input value={o?.t || ''} size={4}
                                               style={{display: 'inline-flex', width: 'unset'}}
                                               onChange={e => tableSetInp(name, i, inp, setInp, 't', e.currentTarget.value || undefined)}/>
                                        (s)，速度={!isNaN(itspd) && floatInterception(itspd, 2)} (m/s)。
                                    </div>;
                                })}
                                <Text variant="h6" css={{display: 'inline'}}>平均速度= {avspeed} m/s,
                                   对照：额定提升速度= {inp?.[ratednm]} (m/s)；速度误差= {avspeed!==undefined? floatInterception(percnt, 0) : ''} (%)</Text>
                                <br/>
                                <div css={{display: 'ruby'}}>
                                    <Text variant="h6">{title}-检验结果：</Text>
                                    <SelectHookfork value={(inp?.[name + 'r']) || ''}
                                                    onChange={e => setInp({...inp, [name + 'r']: e.currentTarget.value || undefined})}/>
                                </div>
                            </React.Fragment>;
                        })}
                    </div>
                </div>
                备注：
                <TextArea  value={inp?.速度备注 ||''} rows={3}
                           onChange={e => setInp({ ...inp, 速度备注: e.currentTarget.value||undefined}) } />
                {children}
            </InspectRecordLayout>
        );
} );

//升降机：多出： 计算误差；
//【表格】 【助手】  JSON.parse(orc?._tblFixed??'[]')  【工具】
export const MoveSpeedLiftVw= ({children, orc, rep,label,config,sseq,fixed= ["%", "5%", "15%", "11%", "13%", "12%", "9%", "9%", "6%"]  }
        : { orc: any, rep: any,label:any, children?: any,config:any[],sseq:number,fixed?:string[]}
) => {
    const config上 = config.slice(0, sseq);
    const config下 = config.slice(sseq);
    return <>
        <div css={{"@media print": {paddingBottom: '7rem', pageBreakInside: 'avoid'}}}>
            {typeof label === 'object' ? <>{label}</>
                :
                <Text variant="h4" css={{
                    marginTop: '1rem',
                }}>{label}</Text>
            }
        </div>
        <Table id={'MoveSpeed'} fixed={ fixed }
               css={{borderCollapse: 'collapse', "@media print": {marginTop: '-7rem'}}} tight miniw={800}>
            <TableHead>
                <TableRow>
                    <CCell>项目</CCell><CCell>次数</CCell><CCell>运行距离(m)</CCell><CCell>运行时间(s)</CCell><CCell>提升速度(m/s)</CCell>
                    <CCell>额定提升速度 (m/s)</CCell><CCell>平均速度(m/s)</CCell><CCell>速度误差(%)</CCell><CCell>检验结果</CCell>
                </TableRow>
            </TableHead>
            <TableBody>
                <RepLink ori rep={rep} tag={'MoveSpeed'}>
                    {config上.map(([name, title, ratednm], t: number) => {
                        const avspeed = calcAverageArrObj(orc?.[name], (row) => row?.d / row?.t, 2);
                        let percnt=avspeed!==undefined? Math.abs((Number(avspeed) - orc?.[ratednm]))*100/orc?.[ratednm] : 0;
                        return <React.Fragment key={t}>
                            {(new Array(3)).fill(null).map((_, d: number) => {
                                const o = orc?.[name]?.[d];
                                const itspd = o?.d / o?.t;
                                return <TableRow key={d}>
                                    {0 === d && <CCell rowSpan={3}>{title}</CCell>}
                                    <CCell>{d + 1}</CCell>
                                    <CCell>{o?.d}</CCell>
                                    <CCell>{o?.t}</CCell>
                                    <CCell>{!isNaN(itspd) && floatInterception(itspd, 2)}</CCell>
                                    {0 === d && <CCell rowSpan={3}>{orc?.[ratednm]}</CCell>}
                                    {0 === d && <CCell rowSpan={3}>{avspeed}</CCell>}
                                    {0 === d && <CCell rowSpan={3}>{avspeed!==undefined? floatInterception(percnt, 0) : ''}</CCell>}
                                    {0 === d && <CCell rowSpan={3}>{orc?.[name + 'r']}</CCell>}
                                </TableRow>;
                            })}
                        </React.Fragment>;
                    })}
                </RepLink>
            </TableBody>
        </Table>
        <Table fixed={ ["6%", "%"] }  css={{borderCollapse: 'collapse', }} tight miniw={800}>
            <TableBody>
                <RepLink ori rep={rep} tag={'MoveSpeed'}>
                    <TableRow>
                        <CCell>备注</CCell>
                        <Cell>
                            <div css={{minHeight: '1rem', whiteSpace: 'pre-wrap'}}>
                                {orc.速度备注 || '／'}
                            </div>
                        </Cell>
                    </TableRow>
                </RepLink>
            </TableBody>
        </Table>
        {children}
    </>;
};
